<template>
  <Tooltip placement="top">
    <template #title>
      <span>导出</span>
    </template>
    <ExportOutlined @click="exportData" />
  </Tooltip>
</template>
<script lang="ts">
  import { defineComponent, unref, computed, ref } from 'vue';
  import { Tooltip } from 'ant-design-vue';
  import { ExportOutlined } from '@ant-design/icons-vue';
  import { useTableContext } from '../../hooks/useTableContext';
  import { useGlobSetting } from '/@/hooks/setting';
  import { downloadByUrl } from '/@/utils/file/download';
  import { isFunction } from '/@/utils/is';

  export default defineComponent({
    name: 'ExportSetting',
    components: {
      ExportOutlined,
      Tooltip,
    },
    setup() {
      const table = useTableContext();
      const getValues = computed(() => {
        return unref(table?.getBindValues) || {};
      });
      function exportData() {
        const values = unref(getValues);
        const { ctxAdminPath } = useGlobSetting();
        // 查询是否有导出配置
        let url = ctxAdminPath + '/common/export?code=' + values.layoutCode;
        if (values.useSearchForm) {
          let searchInfo = values.getFormData();
          // 加上额外参数
          if (values.searchInfo) {
            searchInfo = {
              ...searchInfo,
              ...values.searchInfo,
            };
          }
          //  查询条件请求之前处理
          if (values.handleSearchInfoFn && isFunction(values.handleSearchInfoFn)) {
            searchInfo = values.handleSearchInfoFn(searchInfo);
          }
          // 查询条件请求之前处理
          if (values.beforeFetch && isFunction(values.beforeFetch)) {
            searchInfo = values.beforeFetch(searchInfo);
          }
          url += '&searchData=' + encodeURI(JSON.stringify(searchInfo));
        }
        // 导出
        downloadByUrl({
          url,
          target: '_self',
        });
      }

      return { exportData };
    },
  });
</script>
